package cs.xwi7xwa.activities;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.code.microlog4android.Logger;
import com.google.code.microlog4android.appender.FileAppender;
import com.google.code.microlog4android.config.PropertyConfigurator;
import com.google.code.microlog4android.repository.DefaultLoggerRepository;

import cs.xwi7xwa.R;
import cs.xwi7xwa.templates.BumpTemplate;
import cs.xwi7xwa.templates.Xwi7xwaToast;

public class HomeView extends BumpTemplate
{
    private static final String LOG_FILE = "bumpLog.txt";
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        
        // Set text in the title textview
        ((TextView) findViewById(R.id.Title)).setText(Html.fromHtml("Xwi7xwa<br />Museum App"));
        
        // Setup logger
        logSetup();
        
        // Log this method's execution
        log(null);
    }

    public void goToExhibitsListView(View view)
    {
        Intent intent = new Intent(view.getContext(), ExhibitListView.class);
        startActivityForResult(intent, 0);
        
        // Log this method's execution
        log(null);
    }
    
    public void goToMyFactsView(View view)
    {
        Intent intent = new Intent(view.getContext(), MyFactsView.class);
        startActivityForResult(intent, 0);
        
        // Log this method's execution
        log(null);
    }

    public void onMyButtonClick(View view)
    {
        // Display an error message
        View layout = getLayoutInflater().inflate(R.layout.toast,
                (ViewGroup) findViewById(R.id.toastText));
        Xwi7xwaToast notImplemented = new Xwi7xwaToast(layout,
                getApplicationContext(),
                "Sorry, this feature has not been developed.");
        notImplemented.show();
        
        // Log this method's execution
        log(null);
    }
    
    /**
     * Sets up the logger by importing properties file
     * and add a file appender that outputs logs to LOG_FILE
     */
    private void logSetup()
    {
        PropertyConfigurator.getConfigurator(this).configure();
        
        Logger rootLogger = DefaultLoggerRepository.INSTANCE.getRootLogger();
        FileAppender fileAppender = new FileAppender();
        fileAppender.setFileName(LOG_FILE);
        fileAppender.setAppend(true);
        rootLogger.addAppender(fileAppender);
    }
}